<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>异常处理 | JavaScript高级讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../Sources/调试工具的使用.html" />
    
    
    <link rel="prev" href="../Sources/dom操作复习.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="1.7"
        data-chapter-title="异常处理"
        data-filepath="Sources/异常处理.md"
        data-basepath=".."
        data-revision="Tue Oct 18 2016 20:46:48 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript高级讲义
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.1" data-path="Sources/内容简介.html">
            
                
                    <a href="../Sources/内容简介.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                        内容简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1" >
            
            <span><b>1.</b> JavaScript基础知识复习</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="Sources/基础复习题.html">
            
                
                    <a href="../Sources/基础复习题.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        基础复习题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
            <span><b>1.2.</b> 内置对象与方法</span>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="Sources/array.html">
            
                
                    <a href="../Sources/array.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        Array
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Sources/date.html">
            
                
                    <a href="../Sources/date.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Date
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Sources/number.html">
            
                
                    <a href="../Sources/number.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.3.</b>
                        
                        Number
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="Sources/boolean.html">
            
                
                    <a href="../Sources/boolean.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.4.</b>
                        
                        Boolean
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="Sources/string.html">
            
                
                    <a href="../Sources/string.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.5.</b>
                        
                        String
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="Sources/math.html">
            
                
                    <a href="../Sources/math.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.6.</b>
                        
                        Math
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Sources/值类型和引用类型.html">
            
                
                    <a href="../Sources/值类型和引用类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        值类型和引用类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="Sources/值类型和引用类型的特征.html">
            
                
                    <a href="../Sources/值类型和引用类型的特征.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        值类型和引用类型的特征
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Sources/对象的动态特性.html">
            
                
                    <a href="../Sources/对象的动态特性.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        对象的动态特性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Sources/dom操作复习.html">
            
                
                    <a href="../Sources/dom操作复习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DOM操作复习
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="Sources/异常处理.html">
            
                
                    <a href="../Sources/异常处理.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Sources/调试工具的使用.html">
            
                
                    <a href="../Sources/调试工具的使用.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        调试工具的使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript高级讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5F02;&#x5E38;&#x5904;&#x7406;">&#x5F02;&#x5E38;&#x5904;&#x7406;</h1>
<h2 id="&#x5E38;&#x89C1;&#x7684;&#x5F02;&#x5E38;&#x5206;&#x7C7B;">&#x5E38;&#x89C1;&#x7684;&#x5F02;&#x5E38;&#x5206;&#x7C7B;</h2>
<ul>
<li>&#x8FD0;&#x884C;&#x73AF;&#x5883;&#x7684;&#x591A;&#x6837;&#x6027;&#x5BFC;&#x81F4;&#x7684;&#x5F02;&#x5E38;&#xFF08;&#x6D4F;&#x89C8;&#x5668;&#xFF09;</li>
<li>&#x8BED;&#x6CD5;&#x9519;&#x8BEF;&#xFF0C;&#x4EE3;&#x7801;&#x9519;&#x8BEF;</li>
</ul>
<p>&#x5F02;&#x5E38;&#x6700;&#x5927;&#x7684;&#x7279;&#x5F81;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x65E6;&#x4EE3;&#x7801;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x4E0D;&#x4F1A;&#x518D;&#x6267;&#x884C;</p>
<h2 id="&#x5F02;&#x5E38;&#x6355;&#x83B7;">&#x5F02;&#x5E38;&#x6355;&#x83B7;</h2>
<p>&#x6355;&#x83B7;&#x5F02;&#x5E38;&#xFF0C;&#x4F7F;&#x7528;<code>try-catch</code>&#x8BED;&#x53E5;</p>
<pre><code class="lang-js"><span class="hljs-keyword">try</span>{
    <span class="hljs-comment">//&#x8FD9;&#x91CC;&#x5199;&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x7684;&#x4EE3;&#x7801;</span>
}<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-comment">//&#x8FD9;&#x91CC;&#x7684;e&#x5C31;&#x662F;&#x6355;&#x83B7;&#x7684;&#x5F02;&#x5E38;&#x5BF9;&#x8C61;</span>
    <span class="hljs-comment">//&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x91CC;&#x5199;&#xFF0C;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x540E;&#x7684;&#x5904;&#x7406;&#x4EE3;&#x7801;</span>
}
</code></pre>
<p>&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x8BED;&#x53E5;&#x6267;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4E3A;&#xFF1A;</p>
<ol>
<li><p>&#x4EE3;&#x7801;&#x6B63;&#x5E38;&#x8FD0;&#x884C;, &#x5982;&#x679C;&#x5728;try&#x4E2D;&#x51FA;&#x73B0;&#x4E86;&#x9519;&#x8BEF;, try &#x91CC;&#x9762;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x7684;&#x8BED;&#x53E5;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x90FD;&#x4E0D;&#x518D;&#x6267;&#x884C;, &#x76F4;&#x63A5;&#x8DF3;&#x8F6C;&#x5230; catch &#x4E2D;</p>
</li>
<li><p>catch&#x4E2D;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x4FE1;&#x606F;</p>
</li>
<li><p>&#x7136;&#x540E;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;</p>
</li>
<li><p>&#x5982;&#x679C; try &#x4E2D;&#x6CA1;&#x6709;&#x51FA;&#x73B0;&#x9519;&#x8BEF;, &#x90A3;&#x4E48;&#x4E0D;&#x8D70; catch &#x76F4;&#x63A5;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684;&#x4EE3;&#x7801;</p>
</li>
</ol>
<p>&#x901A;&#x8FC7;<code>try-catch</code>&#x8BED;&#x53E5;&#x8FDB;&#x884C;&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x4E4B;&#x540E;&#xFF0C;&#x4EE3;&#x7801;&#x5C06;&#x4F1A;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x4E2D;&#x65AD;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong></p>
<ul>
<li>&#x8BED;&#x6CD5;&#x9519;&#x8BEF;&#x5F02;&#x5E38;&#x7528;<code>try-catch</code>&#x8BED;&#x53E5;&#x65E0;&#x6CD5;&#x6355;&#x83B7;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x9884;&#x89E3;&#x6790;&#x9636;&#x6BB5;&#xFF0C;&#x8BED;&#x6CD5;&#x9519;&#x8BEF;&#x4F1A;&#x76F4;&#x63A5;&#x68C0;&#x6D4B;&#x51FA;&#x6765;&#xFF0C;&#x800C;&#x4E0D;&#x4F1A;&#x7B49;&#x5230;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x5019;&#x624D;&#x62A5;&#x9519;&#x3002;</li>
<li>try-catch&#x5728;&#x4E00;&#x822C;&#x65E5;&#x5E38;&#x5F00;&#x53D1;&#x4E2D;&#x57FA;&#x672C;&#x7528;&#x4E0D;&#x5230;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x8981;&#x5199;&#x6846;&#x67B6;&#x4EC0;&#x4E48;&#x7684;&#xFF0C;&#x7528;&#x7684;&#x4F1A;&#x975E;&#x5E38;&#x591A;&#x3002;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x4F1A;&#x8BA9;&#x6846;&#x67B6;&#x53D8;&#x5F97;&#x5065;&#x58EE;</li>
</ul>
<h2 id="&#x629B;&#x51FA;&#x5F02;&#x5E38;">&#x629B;&#x51FA;&#x5F02;&#x5E38;</h2>
<p>&#x5982;&#x4F55;&#x624B;&#x52A8;&#x7684;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x5462;&#xFF1F;</p>
<blockquote>
<p>&#x6848;&#x4F8B;&#xFF1A;&#x81EA;&#x5DF1;&#x5199;&#x7684;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x7528;&#x6237;&#x4E0D;&#x4F20;&#x53C2;&#x6570;&#xFF0C;&#x6B64;&#x65F6;&#x60F3;&#x76F4;&#x63A5;&#x7ED9;&#x7528;&#x6237;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x5982;&#x4F55;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x3002;</p>
</blockquote>
<p>&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x4F7F;&#x7528;<code>throw</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">throw</span> &#x5F02;&#x5E38;&#x5BF9;&#x8C61;;
</code></pre>
<p>&#x5F02;&#x5E38;&#x5BF9;&#x8C61;&#x4E00;&#x822C;&#x662F;&#x7528;<code>new Error(&quot;&#x5F02;&#x5E38;&#x6D88;&#x606F;&quot;)</code>, &#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EFB;&#x610F;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test</span>(<span class="hljs-params">para</span>)</span>{
    <span class="hljs-keyword">if</span>(para == <span class="hljs-literal">undefined</span>){
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">&quot;&#x8BF7;&#x4F20;&#x9012;&#x53C2;&#x6570;&quot;</span>);
        <span class="hljs-comment">//&#x8FD9;&#x91CC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5BF9;&#x8C61;</span>
        <span class="hljs-keyword">throw</span> {<span class="hljs-string">&quot;id&quot;</span>:<span class="hljs-number">1</span>, msg:<span class="hljs-string">&quot;&#x53C2;&#x6570;&#x672A;&#x4F20;&#x9012;&quot;</span>};
    }
}

<span class="hljs-keyword">try</span>{
    test();
}<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-built_in">console</span>.log(e);
}
</code></pre>
<h2 id="&#x5F02;&#x5E38;&#x7684;&#x4F20;&#x9012;&#x673A;&#x5236;">&#x5F02;&#x5E38;&#x7684;&#x4F20;&#x9012;&#x673A;&#x5236;</h2>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f1</span> (<span class="hljs-params"></span>) </span>{
    f2(); <span class="hljs-comment">// f1 &#x79F0;&#x4E3A;&#x8C03;&#x7528;&#x8005;, &#x6216;&#x4E3B;&#x8C03;&#x51FD;&#x6570;, f2 &#x79F0;&#x4E3A;&#x88AB;&#x8C03;&#x7528;&#x8005;, &#x6216;&#x88AB;&#x8C03;&#x51FD;&#x6570;</span>
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f2</span> (<span class="hljs-params"></span>) </span>{
    f3();
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f3</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>( <span class="hljs-string">&apos;error&apos;</span> );
}
f1();
</code></pre>
<p>&#x5F53;&#x5728;&#x88AB;&#x8C03;&#x51FD;&#x6570;&#x5185;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5F02;&#x5E38;&#x4F1A;&#x4E00;&#x7EA7;&#x4E00;&#x7EA7;&#x5F80;&#x4E0A;&#x629B;&#x51FA;&#x3002;</p>
<h2 id="&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x8BED;&#x53E5;&#x7684;&#x5B8C;&#x6574;&#x6A21;&#x5F0F;">&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x8BED;&#x53E5;&#x7684;&#x5B8C;&#x6574;&#x6A21;&#x5F0F;</h2>
<p>&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x8BED;&#x53E5;&#x7684;&#x5B8C;&#x6574;&#x6A21;&#x5F0F;&#x4E3A;<code>try-catch-finally</code></p>
<pre><code class="lang-js"><span class="hljs-keyword">try</span> {
    <span class="hljs-comment">//&#x53EF;&#x80FD;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x7684;&#x4EE3;&#x7801;</span>
} <span class="hljs-keyword">catch</span> ( e ) {
    <span class="hljs-comment">//&#x5982;&#x679C;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x5C31;&#x6267;&#x884C;</span>
} <span class="hljs-keyword">finally</span> {
    <span class="hljs-comment">//&#x7ED3;&#x675F; try &#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#x4E4B;&#x524D;&#x6267;&#x884C;, &#x5373;&#x6700;&#x540E;&#x6267;&#x884C;</span>
}
</code></pre>
<p><code>finally</code>&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4E0D;&#x7BA1;&#x6709;&#x6CA1;&#x6709;&#x53D1;&#x751F;&#x5F02;&#x5E38;&#xFF0C;&#x90FD;&#x4F1A;&#x6267;&#x884C;&#x3002;&#x4E00;&#x822C;&#x7528;&#x5728;&#x540E;&#x7AEF;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x7528;&#x6765;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#xFF0C;JavaScript&#x4E2D;&#x5F88;&#x5C11;&#x4F1A;&#x7528;&#x5230;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../Sources/dom操作复习.html" class="navigation navigation-prev " aria-label="Previous page: DOM操作复习"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../Sources/调试工具的使用.html" class="navigation navigation-next " aria-label="Next page: 调试工具的使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
